#include "dialt.h"
#include <QPainter>
#include <QPainterPath>
#include <QtMath>

DialT::DialT(QWidget *parent)
    : QWidget{parent}
{
    m_value = 50;
}

void DialT::setValue(double value)
{
    m_value = value;
    update();
}

void DialT::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    drawBackground(&painter);
    drawRing(&painter);
    drawScale(&painter);
    drawScaleNum(&painter);
    drawPointer(&painter);
    drawValue(&painter);
}

void DialT::drawBackground(QPainter *painter)
{
    int width = this->width();
    int height = this->height();
    //int radius = qMin(width, height);
    QRectF rectangle(0, 0, width,height);
    painter->fillRect(rectangle, QColor(1,1,1)); // 绘制背景色
}

void DialT::drawRing(QPainter *painter)
{

}

void DialT::drawScale(QPainter *painter)
{
    int width = this->width();
    int height = this->height();
    int radius = qMin(width, height) / 2 - 60;
    QRectF rectangle(width/2 -radius, height/2 - radius, 2 * radius, 2 * radius);

    painter->setPen(QPen(QColor(250,25,50), 6));
    int startAngle1 = -30 * 16;
    int spanAngle1 = 45 * 16;
    painter->drawArc(rectangle, startAngle1, spanAngle1);

    painter->setPen(QPen(QColor(250,235,50), 6));
    int startAngle2 = 15 * 16;
    int spanAngle2 = 45 * 16;
    painter->drawArc(rectangle, startAngle2, spanAngle2);

    painter->setPen(QPen(QColor(50,250,50), 6));
    int startAngle3 = 60 * 16;
    int spanAngle3 = 150 * 16;
    painter->drawArc(rectangle, startAngle3, spanAngle3);



    //-----------------------------------------------

    radius = qMin(width, height) / 2 - 55; // 刻度位于进度条内侧
    painter->setPen(QPen(QColor(250,235,250), 2));

    for (int i = 0; i <= 100; i += 10)
    {
        // 从225°开始到-45°结束，确保顺时针方向
        // double angle = -225 + (i / 100.0) * 270;
        // -30 --- 210
        double angle = -210 + (i / 100.0) * 240;
        double radian = qDegreesToRadians(angle);
        int outerX = width/2 + radius * cos(radian);
        int outerY = height/2 + radius * sin(radian);
        int innerX = width/2 + (radius - 10) * cos(radian); // 刻度线长度为10
        int innerY = height/2 + (radius - 10) * sin(radian);

        painter->drawLine(QPointF(innerX, innerY), QPointF(outerX, outerY));

        // 刻度文字
        painter->setFont(QFont("Arial", 20, QFont::Bold));
        QString text = QString::number(i);
        QFontMetrics fm(painter->font());
        int textX = width / 2 + (radius + 15) * cos(radian) - fm.horizontalAdvance(text) / 2;
        int textY = height / 2 + (radius + 15) * sin(radian) + fm.height() / 3;
        painter->drawText(textX, textY, text);

    }

}

void DialT::drawScaleNum(QPainter *painter)
{

}

void DialT::drawPointer(QPainter *painter)
{
    int width = this->width();
    int height = this->height();
    int radius = qMin(width, height) / 2 - 60;

    painter->setPen(QPen(QColor(180,210,251), 6));
    double angle = -210 + (m_value / 100.0) * 240;
    double radian = qDegreesToRadians(angle);
    int outerX = width/2 + radius * cos(radian);
    int outerY = height/2 + radius * sin(radian);
    int innerX = width/2 + (radius - 220) * cos(radian);
    int innerY = height/2 + (radius - 220) * sin(radian);

    painter->drawLine(QPointF(innerX, innerY), QPointF(outerX, outerY));
}

void DialT::drawValue(QPainter *painter)
{
    int width = this->width();
    int height = this->height();
    painter->setPen(QPen(QColor(50,2,250), 1));
    painter->setBrush(QColor(180,210,251));
    painter->drawEllipse(width/2-50,height/2-50, 100, 100);

    painter->setFont(QFont("Arial", 35, QFont::Bold));
    QRectF rectangle(width/2-45,height/2-45, 90, 90);
    painter->drawText(rectangle, Qt::AlignCenter, QString::number(m_value));// + "%"

}











